package utils;

/**
 * DBUtils.java文件提供了获取数据库链接的方法
 * 
 * 2009-10-12 by Jason Yan
 * 
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.sql.CLOB;

/**
 * DBUtils.java类用于提供操作数据库连接的方法
 * 
 * @author jason 2009-10-12
 * @since 1.0
 */
public class DBUtils {

	private static String url;
	
	private static String username;
	
	private static String pwd;
	
	/** ds DataSource 数据源对象 */
	//private static DataSource ds;

	/**
	 * 静态块的作用： 1.获取初始化上下文context 2.通过config.db配置文件获取数据库连接池JNDI名 3.在上下文中查找数据源对象
	 */
	static {
		/*try {
			InitialContext context = new InitialContext();// 注释1			
			String jndi = ConfigUtils.getDBConfig(("jndi")); //注释2
			ds = (DataSource) context.lookup(jndi);// 注释3
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/		
		url = ConfigUtils.getDBConfig("url");
		username = ConfigUtils.getDBConfig("username");
		pwd = ConfigUtils.getDBConfig("pwd");
		try {
			DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	
	

	/**
	 * getConnection静态方法用于从数据源获取Connection对象
	 * 
	 * @return Connection对象
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		Connection con = DriverManager.getConnection(url, username,pwd);
		return con;
	}

	/**
	 * closeCon方法用于关闭所有与数据库操作相关的对象
	 * 
	 * @param con
	 *            Connnection 连接对象
	 * @throws SQLException
	 *             SQL异常
	 */
	public static void CloseCon(Connection con) throws SQLException {
		if (con != null) { // 关闭数据库连接对象
			con.close();
		}
	}

	/**
	 * closeStmt方法用于关闭命令执行对象
	 * 
	 * @param stmt	Statment	命令执行对象
	 * @throws SQLException
	 */
	public static void CloseStmt(Statement stmt) throws SQLException {
		if (stmt != null) { // 关闭命令执行对象
			stmt.close();
			stmt = null;
		}
	}

	/**
	 * CloseRs
	 * 
	 * @param rs
	 * @throws SQLException
	 */
	public static void CloseRs(ResultSet rs) throws SQLException {
		if (rs != null) { // 关闭结果集对象
			rs.close();
			rs = null;
		}
	}
	
	
	/**
	 * getCLOBFromString方法将String转换为orale.sql.CLOB类型
	 * 
	 * @param con
	 * @param str
	 * @return
	 * @throws SQLException
	 */
	public static CLOB getCLOBFromString(Connection con,String str) throws SQLException{
		if(null == str) return null;
		CLOB clob = CLOB.createTemporary(con,true,CLOB.DURATION_SESSION);		
		clob.setString(1L,str);
		return clob;
	}

	/**
	 * getStringFromCLOB方法将从Oracle数据库中读取的CLOB数据转换为String类型
	 * 
	 * @param clob
	 * @return
	 * @throws SQLException
	 * @throws IOException
	 */
	public static String getStringFromCLOB(CLOB clob) throws SQLException, IOException{
		if(null == clob) return null;
		StringBuffer strBuf = new StringBuffer();
		BufferedReader reader = new BufferedReader(clob.getCharacterStream());
		String str = null;
		while((str = reader.readLine()) != null){
			strBuf.append(str);
		}
		return strBuf.toString();
	}
	
	
	public static void main(String[] args) throws SQLException {
		Connection con = DBUtils.getConnection();
		if(con != null){
			System.out.println("OK");
			DBUtils.CloseCon(con);
		}
	}
	
}
